import { all, takeLatest, call, put, select } from 'redux-saga/effects';
import * as service from '../service';
import { createHashHistory } from "history";
const history = createHashHistory();

// 注册
function* register(action) {
  try{
    const res = yield call(service.register, action.param);
    if (res.data.status === 0) {
      localStorage.setItem('token', res.data.token);
      yield put({ type: '#auth' });
      history.push('/');
    } else {
      alert(res.data.message);
    }
  } catch(e){
    console.log(e.message);
  }
}

// 登录
function* login(action) {
  try{
    const res = yield call(service.login, action.param);
    if (res.data.status === 0) {
      localStorage.setItem('token', res.data.token);
      yield put({ type: '#auth' });
      history.push('/');
    }else{
      alert(res.data.message);
    }
  }catch(e){
    console.log(e.message);
  }
}

// 登出
function* logout(action) {
  localStorage.removeItem('token');
  yield put({ type: '#unauth' });
  history.push('/login');
}

function* profile(action) {
  try{
    const token = localStorage.getItem('token');
    const res = yield call(service.profile, { token });
    if (res.data.status === 0) {
      yield put({ type: '#profile', payload: res.data.profile });
    } else {
      alert(res.data.message);
    }
  }catch(e){
    console.log(e.message);
  }
}

// 增
function* create(action) {
  try{
    const { selected, index, record } = action.param;
    const token = localStorage.getItem('token');
    const res = yield call(service.createRecord, { token, selected, record });
    if (res.data.status === 0) {
      yield put({
        type: '#save',
        payload: { index, record: res.data.record }
      });
    } else {
      alert(res.data.message);
    }
  } catch (e) {
    console.log(e.message);
  }

}

// 删
function* remove(action){
  try{
    const { id, index } = action.param;
    const token = localStorage.getItem('token');
    const res = yield call(service.removeRecord, { token, id });
    if (res.data.status === 0) {
      alert(res.data.message);
      yield put({
        type: '#remove',
        payload: { index }
      });
    } else {
      alert(res.data.message);
    }
  }catch(e){
    console.log(e.message);
  }

}

// 改
function* update(action) {
  try{
    const { selected, index, record } = action.param;
    const token = localStorage.getItem('token');
    const getState = (state, index) => state.main.records[index];
    const { _id } = yield select(getState,index);
    record._id = _id;
    const res = yield call(service.updateRecord, { token, selected, record });
    if (res.data.status === 0) {
      yield put({
        type: '#update',
        payload: { index, record }
      });
    }else{
      alert(res.data.message);
    }
  }catch(e){
    console.log(e.message)
  }
}

// 查
function* query(action) {
  try{
    const token = localStorage.getItem('token');
    const { selected } = action.param;
    
    const res = yield call(service.queryRecord, { token, selected });
    if (res.data.status === 0) {
      yield put({
        type: '#query',
        payload: {
          records: res.data.records,
          selected: selected
        }
      });
    }else{
      alert(res.data.message);
    }
  }catch(e){
    console.log(e.message);
  }
}

export default function* rootSaga() {
  yield all([
    yield takeLatest('$login', login),
    yield takeLatest('$logout', logout),
    yield takeLatest('$register', register),
    yield takeLatest('$profile', profile),
    yield takeLatest('$create', create),
    yield takeLatest('$remove', remove),
    yield takeLatest('$update', update),
    yield takeLatest('$query', query)
  ]);
}
